//
//  SchoolDetailsViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/23.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit
import hpple

class SchoolDetailsViewController: KMViewController {

    fileprivate var urlString = ""
    fileprivate var schoolId = ""
    fileprivate let headView = SchoolHeadView()
    
    init(title:String?="",urlString:String?="",schoolId:String) {
        super.init(nibName: nil, bundle: nil)
        
        self.urlString = urlString ?? ""
        self.title = title
        self.schoolId = schoolId
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        createHeadView()

    }
    
    //MARK: 创建头部视图
    fileprivate func createHeadView() {
    
        self.view.addSubview(headView)
        
        let schoolVC = SchoolRootViewController.init(schoolId: self.schoolId)
        schoolVC.schoolDelegate = self
        self.addChildViewController(schoolVC)
        self.didMove(toParentViewController: self)
        
        self.view.addSubview(schoolVC.view)
        
        
        headView.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(self.view)
            make.height.equalTo(RATIO_H(maxNum: 140))
        }
        
        schoolVC.view.snp.makeConstraints { (make) in
            make.top.equalTo(headView.snp.bottom)
            make.left.right.bottom.equalTo(self.view)
        }
        
        getTitleData()
    }
    
    //MARK: 获取标题信息
    fileprivate func getTitleData() {
        
        KMTool.requestGetData(URLString: self.urlString, parameters: nil, success: { (data) in
            
            let doc = TFHpple.init(htmlData: data)
            let imageElements = doc?.search(withXPathQuery: "//section[@class='sb']/img") as NSArray?
            let imageElement = imageElements?.firstObject
            self.headView.image = (imageElement as? TFHppleElement)?.object(forKey: "src")
            
            let textElements = doc?.search(withXPathQuery: "//section[@class='sb']/div/p") as NSArray?
            let textElement = textElements?.firstObject
            self.headView.text = (textElement as? TFHppleElement)?.text()
            
            
        }) {
        
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    



}

extension SchoolDetailsViewController : SchoolRootDelegate {
    
    //MARK: 改变坐标
    func changeScrollHeight(height: CGFloat) {
        
        var y:CGFloat = height
        if height >= 0 {
            
            if height > RATIO_H(maxNum: 140) {
                y = RATIO_H(maxNum: 140)
            }
        }else{
            
            y = 0
        }
        
        headView.snp.remakeConstraints { (make) in
            make.top.equalToSuperview().offset(-y)
            make.left.right.equalTo(self.view)
            make.height.equalTo(RATIO_H(maxNum: 140))
        }
    }
    
    //MARK: 刷新标题
    func updateTitle() {
        
        if headView.image == nil || headView.text == nil {
            
            getTitleData()
        }
    }
    
}
